|
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton. There are some who are critical of the singleton pattern and consider it to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.〔Scott Densmore. (Why singletons are evil ), May 2004〕〔Steve Yegge. (Singletons considered stupid ), September 2004〕〔(Clean Code Talks - Global State and Singletons )〕 ==Common uses== * The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation. *Facade objects are often singletons because only one Facade object is required. *State objects are often singletons. *Singletons are often preferred to global variables because: * *They do not pollute the global namespace (or, in languages with namespaces, their containing namespace) with unnecessary variables.〔Gamma, E, Helm, R, Johnson, R, Vlissides, J: "Design Patterns", page 128. Addison-Wesley, 1995〕 * *They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「singleton pattern」の詳細全文を読む スポンサード リンク
|